package com.daoliuhe.scaffold.service.impl;

import com.daoliuhe.scaffold.mapper.AuthoritiesMapper;
import com.daoliuhe.scaffold.mapper.RoleAuthoritiesRelationMapper;
import com.daoliuhe.scaffold.mapper.UserRoleRelationMapper;
import com.daoliuhe.scaffold.model.RoleAuthoritiesRelation;
import com.daoliuhe.scaffold.service.RoleAuthoritiesRelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class RoleAuthiortiesRelationServiceImpl implements RoleAuthoritiesRelationService {
	
	private static final Logger logger = LoggerFactory.getLogger(RoleAuthiortiesRelationServiceImpl.class);
	@Autowired
	UserRoleRelationMapper userRoleRelationMapper;
	
	@Autowired
	RoleAuthoritiesRelationMapper roleAuthoritiesRelationMapper;
	
	@Autowired
	AuthoritiesMapper authoritiesMapper;

	public Object getRolePageData(RoleAuthoritiesRelation relation) {
		logger.info("getRolePageData,relation:{}",relation);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = roleAuthoritiesRelationMapper.getRolePageCount(relation);
		int curPage = relation.getPage();
		int rows = relation.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			relation.setPage(maxPage);
		}
		map.put("total", total);
		map.put("rows", roleAuthoritiesRelationMapper.getRolePageData(relation));
		return map;
	}
	
	@Override
	public Object getAuthPageData(RoleAuthoritiesRelation relation) {
		logger.info("getAuthPageData,relation:{}",relation);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = roleAuthoritiesRelationMapper.getAuthPageCount(relation);
		int curPage = relation.getPage();
		int rows = relation.getRows();
		int maxPage = (total + rows - 1) / rows;
		if (curPage > maxPage && maxPage > 0) {
			relation.setPage(maxPage);
		}
		map.put("total", total);
		map.put("rows", roleAuthoritiesRelationMapper.getAuthPageData(relation));
		return map;
	}
	
	@Override
	public Object saveRelation(RoleAuthoritiesRelation relation) {
		logger.info("saveRelation,relation:{}",relation);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		// 用户id和角色id联合查询
		if (StringUtils.isEmpty(relation.getAuthId()) || 
				StringUtils.isEmpty(relation.getRoleId())) {
			reason = "权限或角色不能为空!";
		} else if (roleAuthoritiesRelationMapper.select(relation).isEmpty()) {
			int result = roleAuthoritiesRelationMapper.insert(relation);
			if (result == 1) {
				success = true;
			} else {
				reason = "保存失败!";
			}
		} else {
			reason = "角色已存在!";
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object deleteForAuth(String authId, String roleIds) {
		logger.info("deleteForAuth,authId:{},roleIds:{}",authId,roleIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(authId) || StringUtils.isEmpty(roleIds)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(roleIds.split(","));
			roleAuthoritiesRelationMapper.deleteForAuth(authId, list);
			success = true;
		}
		
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object deleteForRole(String authIds, String roleId) {
		logger.info("deleteForRole,authIds:{},roleId:{}",authIds,roleId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(authIds) || StringUtils.isEmpty(roleId)) {
			reason = "参数错误!";
		} else {
			List<String> list = Arrays.asList(authIds.split(","));
			roleAuthoritiesRelationMapper.deleteRelationForRole(list, roleId);
			success = true;
		}
		
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Override
	public Object saveBatchAuthForRole(String authIds, String roleId) {
		logger.info("saveBatchAuthForRole,authIds:{},roleId:{}",authIds,roleId);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(authIds) || StringUtils.isEmpty(roleId)) {
			reason = "参数错误!";
		} else {
			List<String> authList = new ArrayList<String>(Arrays.asList(authIds.split(";")));
			for (String authId : authList) {
				RoleAuthoritiesRelation relation = new RoleAuthoritiesRelation();
				relation.setAuthId(authId);
				relation.setRoleId(roleId);
				if (roleAuthoritiesRelationMapper.select(relation).isEmpty()) {
					roleAuthoritiesRelationMapper.insert(relation);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}
	
	@Override
	public Object saveBatchRoleForAuth(String authId, String roleIds) {
		logger.info("saveBatchRoleForAuth,authId:{},roleIds:{}",authId,roleIds);
		Map<String, Object> json = new HashMap<String, Object>();
		boolean success = false;
		String reason = "";
		if (StringUtils.isEmpty(authId) || StringUtils.isEmpty(roleIds)) {
			reason = "参数错误!";
		} else {
			List<String> roleList = new ArrayList<String>(Arrays.asList(roleIds.split(";")));
			for (String roleId : roleList) {
				RoleAuthoritiesRelation relation = new RoleAuthoritiesRelation();
				relation.setAuthId(authId);
				relation.setRoleId(roleId);
				if (roleAuthoritiesRelationMapper.select(relation).isEmpty()) {
					roleAuthoritiesRelationMapper.insert(relation);
				}
			}
			success = true;
		}
		json.put("success", success);
		json.put("reason", reason);
		return json;
	}

	@Deprecated
	@Override
	public Object saveBatchUser(String userIds, String roleId) {
		logger.info("saveBatchUser,userIds:{},roleId:{}",userIds,roleId);
		return null;
	}
	
}
